﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Framework.Domain;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid;
using System.Reflection;
using DevExpress.XtraGrid.Columns;

namespace CIIS.Common
{
    public partial class SimpleGridControl : BaseGridControl
    {
        public DataUtils dao = DataUtils.Instance;

        Type dataType = typeof(Entity);
        public Type DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

        IList dataList
        {
            get { return DataSource as IList; }
        }

        string keyString;
        public string KeyString
        {
            get { return keyString; }
            set { keyString = value; }
        }
        string keyValue;
        public string KeyValue
        {
            get { return keyValue; }
            set { keyValue = value; }
        }


        public GridView Gv
        {
            get
            {
                if (Views != null && Views.Count > 0)
                {
                    return Views[0] as GridView;
                }
                else
                {
                    return null;
                }
            }
        }

        public SimpleGridControl()
        {
            InitializeComponent();

            InitControls();
        }

        private void InitControls()
        {
            ContextMenuStrip = contextMenuStrip;
            UseEmbeddedNavigator = false;
            Gv.OptionsView.ShowGroupPanel = false;
        }

        public SimpleGridControl(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            InitControls();
        }

        public void InitColumns(string[] captions, string[] fields)
        {
            if (captions == null || fields == null || captions.Length <= 0 || captions.Length != fields.Length)
            {
                return;
            }
            Gv.Columns.Clear();
            for (int i = 0; i < captions.Length; i++)
            {
                GridColumn gc = new GridColumn() { Caption = captions[i], FieldName = fields[i] };
                gc.VisibleIndex = Gv.Columns.Count;
                Gv.Columns.Add(gc);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Entity entity = Gv.GetRow(Gv.FocusedRowHandle) as Entity;
                if (entity != null)
                {
                    if (!string.IsNullOrWhiteSpace(entity.Id))
                    {
                        entity.State.MarkDeleted();
                        dao.Dao.SaveOrUpdate(entity);
                    }
                    dataList.Remove(entity);
                    RefreshDataSource();
                }
            }
            catch
            { }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Entity entity = dataType.Assembly.CreateInstance(dataType.FullName) as Entity;
                Type type = entity.GetType();
                foreach (PropertyInfo pi in type.GetProperties())
                {
                    if (pi.Name == KeyString)
                    {
                        pi.SetValue(entity, keyValue, null);
                        break;
                    }
                }
                dataList.Add(entity);
                DataSource = dataList;
                RefreshDataSource();
            }
            catch
            { }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        public void SaveData()
        {
            try
            {
                for (int i = 0; i < Gv.DataRowCount; i++)
                {
                    Entity entity = Gv.GetRow(i) as Entity;
                    if (entity != null)
                    {
                        if (string.IsNullOrWhiteSpace(entity.Id))
                        {
                            entity.State.MarkNew();
                        }
                        else
                        {
                            entity.State.MarkDirty();
                        }
                        dao.Dao.SaveOrUpdate(entity);
                    }
                }
            }
            catch
            { }
        }
    }
}
